package com.learn.rabbitmq.nine;

import com.learn.rabbitmq.utils.RabbitMqUtils;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * 死信队列
 * <p>
 * 生产者
 */
public class PriorityProducer {
    //普通交换机
    //普通队列
    public static final String NORMAL_QUEUE = "normal_queue";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();

        Map<String, Object> arguments = new HashMap<>();
        //设置最大优先级 不要设置过大 浪费cpu和内存
        arguments.put("x-max-priority", 10);//官方允许范围0-255

        channel.queueDeclare(NORMAL_QUEUE, true, false, false, arguments);
        for (int i = 1; i <= 10; i++) {
            String message = "info" + i;
            if (i == 5) {
                //设置 5 的优先级
                AMQP.BasicProperties basicProperties =
                        new AMQP.BasicProperties()
                                .builder()
                                .priority(5)
                                .build();
                channel.basicPublish("",
                        NORMAL_QUEUE,
                        basicProperties,
                        message.getBytes(StandardCharsets.UTF_8));
            } else {
                channel.basicPublish(
                        "",
                        NORMAL_QUEUE,
                        null,
                        message.getBytes(StandardCharsets.UTF_8));
            }


        }

        System.out.println("发送完成");
    }
}
